package com.zhuoyue.web.controller.system;

import com.zhuoyue.common.core.controller.BaseController;
import com.zhuoyue.common.core.domain.AjaxResult;
import com.zhuoyue.common.core.page.TableDataInfo;
import com.zhuoyue.common.utils.SecurityUtils;
import com.zhuoyue.system.domain.po.SysMovementLinkPo;
import com.zhuoyue.system.domain.vo.SysMovementLinkVo;
import com.zhuoyue.system.service.FileUploadService;
import com.zhuoyue.system.service.ISysMovementLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author zhanghui
 * @date 2021/10/19 14:48
 */
@Controller
@RequestMapping("/movementLink")
public class SysMovementLinkController extends BaseController {
    @Autowired
    private ISysMovementLinkService sysMovementLinkService;

    @Autowired
    private FileUploadService fileUploadService;

    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysMovementLinkVo sysMovementLink) {
        startPage();
        List<SysMovementLinkVo> list = sysMovementLinkService.selectSysMovementLinkList(sysMovementLink);
        return getDataTable(list);
    }


    @GetMapping(value = "/getById/{id}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(sysMovementLinkService.selectSysMovementLinkById(id));
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody SysMovementLinkVo sysMovementLinkVo) {
        SysMovementLinkPo sysMovementLinkPo = new SysMovementLinkPo();
        sysMovementLinkPo.setId(UUID.randomUUID().toString().replace("-", "").toLowerCase());
        sysMovementLinkPo.setDeptId(sysMovementLinkVo.getDeptId());
        sysMovementLinkPo.setTitle(sysMovementLinkVo.getTitle());
        sysMovementLinkPo.setHtmlText(sysMovementLinkVo.getHtmlText());
        sysMovementLinkPo.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysMovementLinkService.insertSysMovementLink(sysMovementLinkPo));
    }

    @PutMapping("/update")
    @ResponseBody
    public AjaxResult edit(@RequestBody SysMovementLinkVo sysMovementLinkVo) {
        SysMovementLinkPo sysMovementLinkPo = new SysMovementLinkPo();
        sysMovementLinkPo.setId(sysMovementLinkVo.getId());
        sysMovementLinkPo.setDeptId(sysMovementLinkVo.getDeptId());
        sysMovementLinkPo.setTitle(sysMovementLinkVo.getTitle());
        sysMovementLinkPo.setHtmlText(sysMovementLinkVo.getHtmlText());
        sysMovementLinkPo.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysMovementLinkService.updateSysMovementLink(sysMovementLinkPo));
    }

    @DeleteMapping("/deleteById/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable String id) {
        SysMovementLinkVo sysMovementLinkVo = sysMovementLinkService.selectSysMovementLinkById(id);
        if (sysMovementLinkVo != null && sysMovementLinkVo.getHtmlText() != null) {
            List<String> ossUrlList = getOssUrlList(sysMovementLinkVo.getHtmlText());
            for (String ossUrl : ossUrlList) {
                fileUploadService.delete(ossUrl);
            }
        }
        return toAjax(sysMovementLinkService.deleteSysMovementLinkById(id));
    }

    @RequestMapping("/getPage/{id}")
    public String test1(@PathVariable String id, Map map) {
        String content = "<h2>暂无数据</h2>";

        SysMovementLinkVo sysMovementLinkVo = sysMovementLinkService.selectSysMovementLinkById(id);
        if (sysMovementLinkVo != null) {
            content = sysMovementLinkVo.getHtmlText();
        }

        map.put("content", content);
        return "movementLink.html";
    }

    /**
     * 获取oss文件的路径
     *
     * @param text 富文本
     * @return text中oss文件路径列表
     */
    private List<String> getOssUrlList(String text) {
        List<String> urlList = new ArrayList<>();
        int nextIndex = 0;
        String targetStr = "https://zhuoyuezn-img.oss-cn-hangzhou.aliyuncs.com/";

        while (true) {
            nextIndex = text.indexOf(targetStr, nextIndex);
            if (nextIndex < 0) {
                break;
            } else {
                String url = text.substring(nextIndex + targetStr.length(), text.indexOf('\"', nextIndex));
                urlList.add(url);
                nextIndex++;
            }
        }

        return urlList;
    }
}
